package m.jclecai.com.fragment;


import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.LottoTrendActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.contents.LotteryType;


/**
 * 走势图
 */
public class LotteryTrendFragment extends BaseFragment {


    @BindView(R.id.rv_recycler_view)
    RecyclerView mRvRecyclerView;

    private ArrayList<LotteryBean> mLotteryBeen;
    private ItemAdapter mItemAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_lottery_trend;
    }

    @Override
    public void initView() {
        mRvRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        mItemAdapter = new ItemAdapter(null);
        mRvRecyclerView.setAdapter(mItemAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        initLotteryBean();

        mItemAdapter.refresh(mLotteryBeen);
    }

    private void initLotteryBean() {
        mLotteryBeen = new ArrayList<>();
        mLotteryBeen.add(new LotteryBean(R.drawable.dltlogo, "超级大乐透", LotteryType.DLT));
        mLotteryBeen.add(new LotteryBean(R.drawable.qxclogo, "七星彩", LotteryType.QXC));
        mLotteryBeen.add(new LotteryBean(R.drawable.types_pls, "排列三", LotteryType.PLS));
        mLotteryBeen.add(new LotteryBean(R.drawable.types_plw, "排列五", LotteryType.PLW));
        mLotteryBeen.add(new LotteryBean(R.drawable.ssqlogo, "双色球", LotteryType.SSQ));
        mLotteryBeen.add(new LotteryBean(R.drawable.sdlogo, "福彩3D", LotteryType.SD));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "江西11选5", LotteryType.JSSYXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.p3logo, "11运夺金", LotteryType.SDXYXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "广东11选5", LotteryType.GDSYXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "上海11选5", LotteryType.SHSYXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "浙江11选5", LotteryType.ZJSYXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.qlclogo, "七乐彩", LotteryType.QLC));
        mLotteryBeen.add(new LotteryBean(R.drawable.ssc, "重庆时时彩", LotteryType.CQSSC));
        mLotteryBeen.add(new LotteryBean(R.drawable.k3logo, "安徽快3", LotteryType.AHKS));
        mLotteryBeen.add(new LotteryBean(R.drawable.k3logo, "湖北快3", LotteryType.HBKS));
        mLotteryBeen.add(new LotteryBean(R.drawable.k3logo, "吉林快3", LotteryType.JLKS));
        mLotteryBeen.add(new LotteryBean(R.drawable.k3logo, "江苏快3", LotteryType.JSKS));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "江苏11选5", LotteryType.JSKS));
        mLotteryBeen.add(new LotteryBean(R.drawable.lottery_sexw, "浙江12选5", LotteryType.ZJSEXW));
        mLotteryBeen.add(new LotteryBean(R.drawable.syx5logo, "湖北11选5", LotteryType.HBXYXW));

        formatList(mLotteryBeen);
    }

    private void formatList(ArrayList<LotteryBean> lotteryBeen) {

        int size = lotteryBeen.size() % 3;
        if (size != 0) {
            for (int i=1;i<size;i++){
                mLotteryBeen.add(new LotteryBean(0,"",0));
            }
        }
    }


    public  class  ItemAdapter extends BaseRecyclerViewAdapter<LotteryBean> {

        public ItemAdapter(List<LotteryBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_menu;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final LotteryBean bean, int position) {

            if (bean.lotteryIcon != 0) {
                holder.getImageView(R.id.iv_icon).setImageResource(bean.lotteryIcon);
            }
            if (!TextUtils.isEmpty(bean.lotteryName)) {
                holder.getTextView(R.id.tv_name).setText(bean.lotteryName);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(bean.lotteryName)){
                        //这里表示彩票的名字为空的话，就不执行任何操作；
                        return;
                    }
                    //跳转
                    startActivity(new Intent(getActivity(), LottoTrendActivity.class)
                            .putExtra("title", bean.lotteryName)
                            .putExtra("lotteryId", bean.lotteryId));
                }
            });
        }
    }


    public static class LotteryBean {
        public int lotteryIcon;
        public String lotteryName;
        public int lotteryId;

        public LotteryBean(int lotteryIcon, String lotteryName, int lotteryId) {
            this.lotteryIcon = lotteryIcon;
            this.lotteryName = lotteryName;
            this.lotteryId = lotteryId;
        }
    }
}
